#include "gMobilab.h"
using namespace gMobilabPlus;

gMobilab::gMobilab(void)
{
	Instrument::Instrument();

	Resolution = 500.0f/65536.0f;
	DigitalOffset = 0;
	DigitalMax	=	32768;
	DigitalMin	=	-32768;
	
	MyName = _("GMoblab Plus - GTec");
	hDevice = NULL;
	Labels[0] = wxString::Format(wxT("EEG1"));
	Labels[1] = wxString::Format(wxT("EEG2"));
	Labels[2] = wxString::Format(wxT("EEG3"));
	Labels[3] = wxString::Format(wxT("EEG4"));
	Labels[4] = wxString::Format(wxT("EEG5"));
	Labels[5] = wxString::Format(wxT("EEG6"));
	Labels[6] = wxString::Format(wxT("EEG7"));
	Labels[7] = wxString::Format(wxT("EEG8"));
#ifdef HAVE_GTEC_LIBRARY
	hinstLib = LoadLibrary(_("gMOBIlabplus.dll"));
	if (hinstLib != NULL)
	{
		hGT_OpenDevice			=(LibGT_OpenDevice)			GetProcAddress(hinstLib, "GT_OpenDevice");
		hGT_CloseDevice			=(LibGT_CloseDevice)		GetProcAddress(hinstLib, "GT_CloseDevice");
		hGT_InitChannels		=(LibGT_InitChannels)		GetProcAddress(hinstLib, "GT_InitChannels");
		hGT_StartAcquisition	=(LibGT_StartAcquisition)		GetProcAddress(hinstLib, "GT_StartAcquisition");
		hGT_StopAcquisition		=(LibGT_StopAcquisition)		GetProcAddress(hinstLib, "GT_StopAcquisition");
		hGT_GetData				=(LibGT_GetData)				GetProcAddress(hinstLib, "GT_GetData");
		hGT_GetDeviceStatus		=(LibGT_GetDeviceStatus)		GetProcAddress(hinstLib, "GT_GetDeviceStatus");
		hGT_SetTestmode			=(LibGT_SetTestmode)			GetProcAddress(hinstLib, "GT_SetTestmode");
		hGT_GetLastError		=(LibGT_GetLastError)			GetProcAddress(hinstLib, "GT_GetLastError");
	}else
	{
		wxMessageBox(_("Can't find gMOBIlabplus.dll"));
	}
#else
		wxMessageBox(_("This build doesn't support GMobilab Plus"));
#endif
	Initialize(8,256,20);
}

gMobilab::~gMobilab(void)
{
	if(hinstLib != NULL) 
	{
		FreeLibrary(hinstLib);
		hinstLib = NULL;
	}
}

void* gMobilab::Entry(void)
{
#ifdef HAVE_GTEC_LIBRARY
	BUFFER_ST datbuffer;
	BOOL ret;
	int Index = 0;
	
	SampleStruct	Signal;
	memset(&Signal,0,sizeof(Signal));
	Signal.Data.DataLen	=	Channels;
	
	OVERLAPPED ov;
	DWORD dwBytesReceived;
	short *pfl = NULL;
	int dispChannel = 0;
	
	datbuffer.validPoints = 0;
	datbuffer.size = sizeof(buffer);
	datbuffer.pBuffer = &buffer[0];

	m_hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
	ov.hEvent = m_hEvent;
	ov.Offset = 0;
	ov.OffsetHigh = 0;
	Esci = false;

	while(!Esci)
	{
		if(IsRunning)
		{
			Index = 0;
			ret = hGT_GetData(hDevice, &datbuffer, &ov);
			WaitForSingleObject(m_hEvent,1000);
			GetOverlappedResult(hDevice,&ov,&dwBytesReceived,FALSE);
			dwBytesReceived = dwBytesReceived/2;
			while(Index < (int)dwBytesReceived)
			{
				for(int i = 0;i<Channels;i++)
				{
					Signal.Data.Signal[i] = buffer[Index+i];
					Signal.Data.Signal[i] = (Signal.Data.Signal[i]);
				}
				Index +=Channels;
				PutSample(&Signal);					
			}

			
		}else
		{
			Sleep(100);		
		}
	}

	hGT_StopAcquisition(hDevice);
	hGT_CloseDevice(hDevice);
	CloseHandle(m_hEvent);
#endif
	return NULL;
}
void gMobilab::StartDevice(wxString FileName,wxString FilePath,bool File)
{
#ifdef HAVE_GTEC_LIBRARY
	Instrument::StartDevice(FileName,FilePath,File);

	BOOL ret;
	gMobilabPlus::AIN aCh;
	gMobilabPlus::_DIO dCh;

//	int length,i;
//	size_t c;
//	char headerstart[600];
	char * filename = new char[9];
	__CFG * cfg = new __CFG;
	
	//set the channels used and the directions of the DIOS
	aCh.ain1 = TRUE;
	aCh.ain2 = TRUE;
	aCh.ain3 = TRUE;
	aCh.ain4 = TRUE;
	aCh.ain5 = TRUE;
	aCh.ain6 = TRUE;
	aCh.ain7 = TRUE;
	aCh.ain8 = TRUE;

	dCh.dio1_enable = FALSE;
	dCh.dio2_enable = FALSE;
	dCh.dio3_enable = FALSE;
	dCh.dio4_enable = FALSE;
	dCh.dio5_enable = FALSE;
	dCh.dio6_enable = FALSE;
	dCh.dio7_enable = FALSE;
	dCh.dio8_enable = FALSE;

	dCh.dio4_direction = FALSE;
	dCh.dio5_direction = FALSE;
	dCh.dio6_direction = FALSE;
	dCh.dio7_direction = FALSE;
	
	//Set device to testmode if selected
	ret = hGT_SetTestmode(hDevice, false);
	/*if (!ret)
	{
		ErrorHandling();
		return;
	}*/
	ret = hGT_InitChannels(hDevice, aCh, dCh);
	if (!ret)
	{
		//ErrorHandling();
		return;
	}
	ret = hGT_StartAcquisition(hDevice);
	IsRunning = true;
#endif
}
void gMobilab::StopDevice(void)
{
	IsRunning = false;
	Sleep(500);
	Esci = true;
	Instrument::StopDevice();


}
int gMobilab::OpenPort(const char *PortName, const char *OpenVal2, const char *OpenVal3)
{
#ifdef HAVE_GTEC_LIBRARY
	char TempName[256];
	BOOL ret = FALSE;
	UINT	 ErrCode;
	__DEVICESTATE DevStatus;

	//open the connnection to g.MOBIlab+

	strcpy(TempName,PortName);
	hDevice = hGT_OpenDevice(TempName);

	if (hDevice == NULL)
	{
		//ErrorHandling();
		return -1;
	}
	//get the status of the device, to see if device is running
	ret = hGT_GetDeviceStatus(hDevice, &DevStatus);
	if (!ret)
	{
		hGT_GetLastError(&ErrCode);
		if (ErrCode == 15)
		{}
		else
		{
			CloseHandle(hDevice);
			hDevice = NULL;
			
			return -2;
		}
	}
	return 1;
#endif
#ifndef HAVE_GTEC_LIBRARY
	return -1;
#endif
	


}

